import numpy as np

a = np.arange(6).reshape(2, 3)
print('原始数组是：')
print(a)
print('\n')
print('迭代输出元素：')
for x in np.nditer(a):
    print(x, end=", ")
print('\n')

print(a.T)
print('\n')

for x in np.nditer(a.T):
    print(x, end=',')
print('\n')

# 从上述例子可以看出，a 和 a.T 的遍历顺序是一样的，也就是他们在内存中的存储顺序也是一样的，
# 但是 a.T.copy(order = 'C') 的遍历结果是不同的，那是因为它和前两种的存储方式是不一样的，默认是按行访问。
for x in np.nditer(a.T.copy(order="C")):
    print(x, end=",")
print('\n')

# 控制遍历顺序
# for x in np.nditer(a, order='F'):Fortran order，即是列序优先；
# for x in np.nditer(a.T, order='C'):C order，即是行序优先；
a = np.arange(0, 60, 5).reshape(3, 4)
print(a)

b = a.T
print(b)

c = b.copy(order='C')
print(c)

for x in np.nditer(c):
    print(x, end=",")

print('\n')

c = b.copy(order='F')
print(c)
for x in np.nditer(c):
    print(x, end=',')

a = np.arange(0, 60, 5).reshape(3, 4)
print(a, end="\n")
# 修改数组中元素的值
for x in np.nditer(a, op_flags=['readwrite']):
    x[...] = 2 * x
print(a, end='\n')

a = np.arange(0, 60, 5)
a = a.reshape(3, 4)
print(a, end="\n")
for x in np.nditer(a, flags=['external_loop'], order='F'):
    print(x, end=',')

b = np.array([1, 2, 3, 4], dtype=int)
print(b, end='\n')
for x, y in np.nditer([a, b]):
    print(f'{x}:{y}', end=',')
